<template>
  <div :id="id" :class="className" :style="{height:height,width:width}" />
</template>

<script>
import * as echarts from 'echarts'
import resize from './mixins/resize'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '400px'
    },
    titleText: String,
    dataObj: Object
  },
  data () {
    return {
      chart: null
    }
  },
  watch: {
    dataObj (val) {
      this.chart && this.chart.setOption({
        xAxis: {
          data: val.xdata
        },
        series: [{
          data: val.ydata,
          type: 'bar'
        }]
      })
    }

  },
  mounted () {
    this.initChart()
  },
  beforeDestroy () {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart () {
      this.chart = echarts.init(document.getElementById(this.id))
      const xAxisData = []
      const data = []
      this.chart.setOption({
        title: {
          text: this.titleText
        },
        grid: {
          left: '3%',
          right: '3%',
          bottom: '6%'
        },
        tooltip: {
          show: true,
          trigger: 'axis',
          axisPointer: {
            type: 'shadow'
          }
        },
        xAxis: {
          type: 'category',
          axisLabel: {
            show: false
          },
          data: xAxisData
        },
        yAxis: {
          type: 'value'
        },
        series: [{
          data: data,
          type: 'bar'
        }]

      })
    }
  }
}
</script>
